<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Category Admin Controller
 *
 * @package		SimplyPost
 * @subpackage	Category
 * @category	Controller
 * @author		Pascal Kriete
 */

class Admin extends Administrator {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Admin()
	{
		parent::Administrator();
		$this->lang->load('category');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Has no real function - redirects
	 *
	 * @access	public
	 */
	function index()
	{
		redirect('admin/');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Edit the category name through an ajax call
	 *
	 * @access	public
	 * @param	integer	category id
	 */
	function edit($id)
	{
		if (!AJAX)
			show_404();

		$this->load->helper('form');
		$this->load->library('validation');

		$rules['name'] = "trim|required|xss_clean|callback__verify_edit";
		$this->validation->set_rules($rules);

		$fields['name'] = 'Name';
		$this->validation->set_fields($fields);
		
		$this->load->model('Cat_model', 'Category');
		$cat = $this->Category->get($id);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->validation->run() == FALSE)
		{
			echo 'Invalid Input';
		}
		else
		{
			$this->Category->update($id, $_POST['name']);
			echo $_POST['name'];
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Validation callback for edit()
	 *
	 * @access	private
	 * @param	string	new name input
	 * @return 	bool	validation pass/fail
	 */
	function _verify_edit($data)
	{
		$this->load->model('Cat_model', 'Category');
		if($this->Category->exists($data))
		{
			$this->validation->set_message('_verify', 'Table exists');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Add a category
	 *
	 * @access	public
	 */
	function add()
	{
		set_title($this->lang->line('category_add_title'));
		$this->breadcrumb->from_path();
		
		$this->load->helper('form');
		$this->load->library('validation');

		$rules['name'] = "trim|required|xss_clean|callback__verify";
		$this->validation->set_rules($rules);

		$fields['name'] = 'Name';
		$this->validation->set_fields($fields);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->validation->run() == FALSE)
		{
			$this->load->view('admin/add');
		}
		else
		{
			$this->session->set_flashdata('msg', $this->lang->line('cat_added'));
			redirect('home/');
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Validation callback for add()
	 *
	 * @access	private
	 * @param	string	new category name
	 * @return	bool
	 */
	function _verify($data)
	{
		$this->load->model('Cat_model', 'Category');
		if($this->Category->exists($data))
		{
			$this->validation->set_message('_verify', 'Table exists');
			return FALSE;
		}
		else
		{
			$this->Category->create($data);
			return TRUE;
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Delete the category
	 *
	 * @access	public
	 * @param	integer	category id
	 */
	function delete($id)
	{
		$this->load->model('Cat_model', 'Category');
		$this->Category->delete($id);
		
		$this->session->set_flashdata('msg', $this->lang->line('cat_deleted'));
		redirect('/');
	}
}